package com.yungam.imcs.controller.efficiency;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yungam.common.bean.process.ProductProcess;
import com.yungam.common.bean.process.ProductionProcess;
import com.yungam.common.bean.product.ProductInfo;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.ProductInfoService;
import com.yungam.common.service.ProductProcessService;
import com.yungam.common.service.ProductionProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "效率统计页面接口")
@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private ProductProcessService productProcessService;
    @Resource
    private ProductionProcessService productionProcessService;

    @ApiOperation("获取所有的产品信息")
    @GetMapping("/product")
    public RetDTO<?> getAllProducts() {
        List<ProductInfo> list = productInfoService.list();
        return RetDTO.data(list);
    }

    /* 获取产品的工序, 生成表头 */
    @ApiOperation("获取表头")
    @GetMapping("/getTableHead")
    public RetDTO<?> getTableHead(@ApiParam("产品号") String productNo) {
        Map<String, Object> ans = Maps.newHashMap();

        // 保证工序的顺序, 使用 LinkedHashMap
        Map<String, String> heads = Maps.newLinkedHashMap();

        // 根据 productNo 查询表头
        List<ProductProcess> processes = productProcessService.list(new QueryWrapper<ProductProcess>()
                .eq("product_no", productNo).orderByAsc("process_id"));
        // 生成表头
        for (ProductProcess process : processes) heads.put(process.getProcessNo(), process.getProcessName());

        // 组装数据
        ans.put("tableHeads", heads);
        return RetDTO.data(ans);
    }

    @ApiOperation("获取表格数据")
    @GetMapping("/getTableData")
    public RetDTO<?> getDayEfficiency(@ApiParam("产品号") String productNo) {
        List<Map<String, Object>> ans = Lists.newArrayList();

        // 查询各工序所有的耗时信息
        List<ProductionProcess> list = productionProcessService
                .list(new QueryWrapper<ProductionProcess>().eq("product_no", productNo).orderByDesc("start_time"));

        // 计算工序耗时
        List<Map<String, Object>> newList = Lists.newArrayList();

        for (ProductionProcess pp : list) {
            Map<String, Object> item = Maps.newHashMap();
            String cumTime = calculate(pp.getStartTime(), pp.getEndTime());
            item.put("productSn", pp.getProductSn());
            item.put("processNo", pp.getProcessNo());
            item.put("cumTime", cumTime);
            newList.add(item);
        }

        // 整合数据
        Map<String, List<Map<String, Object>>> map = Maps.newHashMap();

        for (Map<String, Object> item : newList) {
            String productSn = String.valueOf(item.get("productSn"));
            List<Map<String, Object>> cur = map.getOrDefault(productSn, Lists.newArrayList());
            cur.add(item);
            map.put(productSn, cur);
        }

        for (String k : map.keySet()) {
            List<Map<String, Object>> maps = map.get(k);
            Map<String, Object> data = Maps.newHashMap();
            data.put("productSn", k);
            for (Map<String, Object> obj : maps) data.put(String.valueOf(obj.get("processNo")), obj.get("cumTime"));
            ans.add(data);
        }

        return RetDTO.data(ans);
    }

    /* 计算耗时 */
    private String calculate(Date startTime, Date endTime) {
        if (endTime == null) return null; // 工序未完成

        long start = startTime.getTime(), end = endTime.getTime(), diff = (end - start) / 1000;
        if (diff < 0) return null;  // 开始时间大于结束时间（过站后又重新进站）

        // 转成相应格式 5h4m30s
        long hour, min, seconds;

        hour = diff / (60 * 60);
        diff %= (60 * 60);

        min = diff / 60;
        seconds = diff % 60;

        String str = "";

        if (hour > 0) str += hour + "h";
        str += min + "m";
        str += seconds + "s";

        return str;
    }

}
